﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace UCenter.Core.Validations
{
    public static class ModelStateDictionaryExtensions
    {
        public static void AttachValidationErrors(this ModelStateDictionary modelState, ValidationResults results)
        {
            foreach (var result in results)
            {
                modelState.AddModelError(
                    result.Key,
                    //result.Target.GetPropertyValue(result.Key).ToString(),
                    result.Message);
                //modelState.AddModelError(
                //    result.Key,
                //    result.Target.GetPropertyValue(result.Key).ToString(),
                //    result.Message);
            }
        }

        public static object GetPropertyValue(this object obj, string propertyName)
        {
            PropertyInfo property = obj.GetType().GetProperty(propertyName);

            return property.GetValue(obj, null);
        }
    }
}
